package com.ai.bitcoin.controller;

import com.ai.bitcoin.exception.BusinessException;
import com.ai.bitcoin.jpa.dao.BccSubCoinDao;
import com.ai.bitcoin.jpa.dao.BccUserWeixinGroupDao;
import com.ai.bitcoin.jpa.dao.BccUserWeixinGroupInviteDao;
import com.ai.bitcoin.jpa.dao.ParameterDao;
import com.ai.bitcoin.jpa.entity.BccUser;
import com.ai.bitcoin.jpa.entity.Parameter;
import com.ai.bitcoin.jpa.service.UserBonusService;
import com.ai.bitcoin.service.BccPageService;
import com.ai.bitcoin.service.UserService;
import com.ai.bitcoin.vo.BccPageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
@Slf4j
public class ApiController {
    @Autowired
    private UserService userService;
    @Autowired
    private BccSubCoinDao bccSubCoinDao;
    @Autowired
    private UserBonusService userBonusService;
    @Autowired
    private ParameterDao parameterDao;
    @Autowired
    private BccUserWeixinGroupDao weixinGroupDao;
    @Autowired
    private BccUserWeixinGroupInviteDao bccUserWeixinGroupInviteDao;
    @Autowired
    private BccPageService bccPageService;

    /**
     * 取得bcc线图数据
     *
     * @param type  1m 5m 10m 30m 1h 1d 7d 1m 1y
     * @param bccId bcc
     * @return
     * @throws BusinessException
     */
    @GetMapping(value = "bcc/point/{type}/{keyLine}/{bccId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<BccPageVo> bccPageByType(@PathVariable String type, @PathVariable String keyLine, @PathVariable String bccId) throws BusinessException {
        BccPageVo vo = bccPageService.bccPageByType(type, keyLine, bccId);
        return new ResponseEntity(vo, HttpStatus.OK);
    }

    /**
     * 测试用户
     *
     * @return
     * @throws BusinessException
     */
    @GetMapping(value = "user/test", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<List<BccUser>> getTestUser() throws BusinessException {
        Parameter parameter = parameterDao.findByParameterKey("TEST_USER_CONFIG").orElseThrow(() -> new BusinessException("400", "参数TEST_USER_CONFIG未配置"));
        List<BccUser> user = userService.getBccUserDao().findByOpenIdIn(StringUtils.split(parameter.getParameterValue(), ","));
        return new ResponseEntity(user, HttpStatus.OK);
    }

    /**
     * 清除测试用户数据
     *
     * @param openId
     * @return
     */
    @PostMapping(value = "user/test/clearData/{openId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Transactional
    public ResponseEntity<String> clearTestUserData(@PathVariable String openId) {
        try {
            BccUser user = userService.getBccUserDao().findByOpenId(openId);
            if (user != null) {
                userService.getBccUserDao().deleteById(user.getId());
                //删除相关唯一的数据
                if (StringUtils.isNotEmpty(user.getNickname())) {
                    userBonusService.getBccUserBonusLogDao().deleteByUniqueKeyLike("_" + user.getNickname());
                }
                userBonusService.getBccUserBonusLogDao().deleteByUniqueKeyLike("_" + user.getOpenId());
                userBonusService.getBccUserBonusLogDao().deleteByUniqueKeyLike("_" + user.getId());
            }
            bccSubCoinDao.deleteByUserOpenId(openId);
            userBonusService.getBccUserBonusLogDao().deleteByOpenId(openId);
            weixinGroupDao.deleteByOpenId(openId);
            bccUserWeixinGroupInviteDao.deleteByOpenId(openId);
            return new ResponseEntity("删除记录完成", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
